package com.itheima_5;

import javafx.scene.input.DataFormat;

import java.io.*;
import java.text.DecimalFormat;

public class Clon extends Thread {
    private File srcFile;
    private File desFile;

    public Clon(String srcFile, String desFile) {
        this.srcFile = new File(srcFile);
        this.desFile = new File(desFile);
    }

    @Override
    public void run() {
        BufferedInputStream bis = null;
        BufferedOutputStream bos=null;
        try {
            bis=new BufferedInputStream(new FileInputStream(srcFile));
            bos=new BufferedOutputStream(new FileOutputStream(desFile));
            int len;
            int conut = bis.available();
            long length = srcFile.length();
            byte[]a=new byte[1024*10];
            int leng=0;
            DecimalFormat df=new DecimalFormat("##%");
            while ((len=bis.read(a))!=-1){
                Thread.sleep(1000);
                bos.write(a,0,len);
                leng+=len;
                System.out.println(srcFile.getName()+"文件复制了："+df.format(1.0*leng/conut));
            }
            System.out.println(srcFile.getName()+"文件复制完毕了");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
